package com.ttx.pattern.example.state.after.states;

import com.ttx.pattern.example.state.after.GumballMachine;
import com.ttx.pattern.example.state.after.State;

/**
 * 糖果机售出糖果
 * @author TimFruit
 * @date 2019/5/19 13:30
 */

public class SoldState implements State {
    private GumballMachine machine;

    public SoldState(GumballMachine gumballMachine) {
        this.machine = gumballMachine;
    }
    
    @Override public void insertQuarter() { //当前状态下，不恰当的行为
        
    }

    @Override public void ejectQuarter() { //当前状态下，不恰当的行为
        
    }

    @Override public void turnCrank() { //当前状态下，不恰当的行为
        
    }

    @Override public void dispense() {
        int count=this.machine.getCount();
        if(count>0){
            System.out.println("发放1个糖果...");
            count=count-1;
            this.machine.setCount(count);
            this.machine.setCurrState(this.machine.getNoQuarterState());
        }else {
            this.machine.setCurrState(this.machine.getSoldOutState());
        }
    }
}
